﻿using System.Collections.Generic;
using System.Linq;

namespace WpfApplication1
{
    /// <summary>
    /// Queries is a list of methods interacting with the database
    /// </summary>
    static class Queries
    {
        static public cvesjrueEntities5 context = new cvesjrueEntities5();

        /// <summary>
        /// Gets all the assignments from the database
        /// </summary>
        public static List<Assignment> GetAssignments()
        {
            var assignments = from a in context.assignment
                              select a;

            List<Assignment> ass = new List<Assignment>();

            foreach (var assignment in assignments)
            {
                Assignment ass2 = new Assignment();
                ass2.Name = assignment.assName;
                ass2.Id = assignment.id;
                ass.Add(ass2);
            }
            ass = SortAssignments(ass);
            return ass;
        }

        /// <summary>
        /// Deletes an assignment by being given a certain assignment Id. Calls the method deleteElement prior
        /// to deleting the assignment however by giving this Id.
        /// </summary>
        public static void DeleteAssignment(int id)
        {
            var deleteAssignment = from assignment in context.assignment
                                   where assignment.id == id
                                   select assignment;
            DeleteElement(id);
            foreach (assignment a in deleteAssignment)
            {
                context.assignment.DeleteObject(a);
            }
            context.SaveChanges();
        }

        /// <summary>
        /// Deletes all elements containing a given assignment Id as foreign key. For each element
        /// deleted this way the method calls the method deleteBoguses prior to deleting the element
        /// by giving the element Id.
        /// </summary>
        public static void DeleteElement(int id)
        {
            var deleteElement = from element in context.element
                                where element.assId == id
                                select element;
            foreach(var e in deleteElement.ToList())
            {
                DeleteBoguses(e.id);
                context.element.DeleteObject(e);
            }
            context.SaveChanges();
        }

        /// <summary>
        /// Deletes all bogus elements containing a given element Id as foreign key.
        /// </summary>
        public static void DeleteBoguses(int id)
        {
            var deleteBogus = from b in context.bogus
                              where b.elementId == id
                              select b;
            foreach(var b in deleteBogus)
            {
                context.bogus.DeleteObject(b);
            }
            context.SaveChanges();
        }

        /// <summary>
        /// Gets an assignment using an assignment Id
        /// </summary>
        public static assignment GetAssignment(int id)
        {
            var ass = from a in context.assignment
                      where a.id == id
                      select a;
            return ass.FirstOrDefault();
        }

        /// <summary>
        /// Gets a list of elements using an assignment Id
        /// </summary>
        public static List<element> GetElements(int assId)
        {
            var elements = from e in context.element
                           where e.assId == assId
                           select e;
            List<element> list1 = new List<element>();

            return new List<element>(SortElements(elements.ToList<element>()));
        }

        /// <summary>
        /// Sorts the assignments using their Id
        /// </summary>
        public static List<Assignment> SortAssignments(List<Assignment> assignments)
        {
            List<int> newList = new List<int>();
            foreach (Assignment a in assignments)
                newList.Add(a.Id);
            newList.Sort();
            List<Assignment> newList1 = new List<Assignment>();
            foreach(int i in newList)
            {
                foreach (Assignment a in assignments)
                {
                    if (a.Id == i)
                    {
                        newList1.Add(a);
                    }
                }
            }
            return newList1;
        }

        /// <summary>
        /// Sorts the elements using their id
        /// </summary>
        public static List<element> SortElements(List<element> elements)
        {
            List<int> newList = new List<int>();
            foreach (element e in elements)
                newList.Add(e.id);
            newList.Sort();
            List<element> newList1 = new List<element>();
            foreach (int i in newList)
            {
                foreach (element e in elements)
                {
                    if (e.id == i)
                    {
                        newList1.Add(e);
                    }
                }
            }
            return newList1;
        }

        /// <summary>
        /// Gets all boguses using elementId as foreign key
        /// </summary>
        public static List<bogus> GetBogus(int elementId)
        {
            var boguses = from b in context.bogus
                          where b.elementId == elementId
                          select b;
            return boguses.ToList<bogus>();
        }

        /// <summary>
        /// Creates a new assignment using the class Assignment. Calls createElement
        /// </summary>
        /// <param name="assignment"></param>
        public static void CreateAssignment(Assignment assignment)
        {
            string assType;
            assType = assignment.GetType() == 0 ? "QUIZ" : "SPELL";
            var ass = new assignment
                          {
                              assName = assignment.Name,
                              text = assignment.AssignmentText,
                              type = assType
                              
                          };
            context.assignment.AddObject(ass);
            context.SaveChanges();

            foreach(Answer a in assignment.GetAnswerlist())
            {
                CreateElement(a, ass.id);
            }
        }

        /// <summary>
        /// Creates elements using the Answer objects from the Assignment Objects list. Calls createBogus
        /// </summary>
        public static void CreateElement(Answer answer, int assId1)
        {
            var ele = new element
                          {
                              assId = (sbyte)assId1,
                              element1 = answer.Realanswer
                          };

            context.element.AddObject(ele);
            context.SaveChanges();
            
            foreach(string s in answer.GetBogusAnswers())
            {
                CreateBogus(s, ele.id);
            }
        }

        /// <summary>
        /// Creates a bogus element using the string from the stringList in the object Answer
        /// </summary>
        /// <param name="bogusForThis"></param>
        /// <param name="eleId"></param>
        public static void CreateBogus(string bogusForThis, int eleId)
        {
            var bogus = new bogus
                            {
                                element = bogusForThis,
                                elementId = (sbyte)eleId
                            };
            context.bogus.AddObject(bogus);
            context.SaveChanges();
        }
    }
}
